package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddNeedQuoteModel extends AndroidViewModel {
    public MutableLiveData<String> stringLiveData = new MutableLiveData<>();
    public MutableLiveData<String> addQuoteLiveData = new MutableLiveData<>();
    public MutableLiveData<Integer> showLoading = new MutableLiveData<>();
    public MutableLiveData<String> toastUntil = new MutableLiveData<>();


    public AddNeedQuoteModel(@NonNull Application application) {
        super(application);
    }

    /****
     * 上传单张图片
     * @param file
     * @param completeNum
     */
    public void uploadSingleImage(Context context, File file, int completeNum, boolean isEnd) {
        Map<String, RequestBody> photos = new HashMap<>(1);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        photos.put("file" + "\"; filename=\"" + completeNum + "image.jpg\"", requestBody);
        DialogHelper.showProgressDialog(context, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUploadAPI().uploadFileV1(photos).enqueue(new Callback<BaseEntity<List<String>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<String>>> call, Response<BaseEntity<List<String>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (response.body().getData() != null && response.body().getData().size() != 0) {
                        stringLiveData.postValue(response.body().getData().get(0));
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<String>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 上传报价
     *
     * @param map
     */
    public void uploadQuote(Map<String, Object> map) {
        showLoading.postValue(1);
        APIManager.getInstance().getNeedAPI().publishNeedQuote(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                showLoading.postValue(2);
                if (response != null && response.body() != null && response.body().getState() != null) {
                    addQuoteLiveData.postValue(response.body().getState());
                } else if (response.body() != null && response.body().getMsg() != null) {
                    toastUntil.postValue(response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                showLoading.postValue(2);
                addQuoteLiveData.postValue("fail");
                toastUntil.postValue(t.getMessage());
            }
        });
    }


}
